<%@page contentType="text/html; charset=utf-8"%>

<div class="form-container">
	<form id="form">
		<table class="tableform">
			<tr>
				<th class="td100">所属菜单：</th>
				<td>
					<input id="menuDialog" name="menuId" class="easyui-validatebox" type="text" style="width: 269px; height:25px;" />
				</td>
			</tr>
			<tr>
				<th>父节点：</th>
				<td>
					<input id="pid" name="pid" class="easyui-validatebox" type="text" style="width: 269px; height:25px;" />
				 	<a onClick="$parentResource.combotree('setValue', '');$('input[name=parentIds]').val('');$('input[name=parentIdentity]').val('')" class="easyui-linkbutton ml-3" data-options="plain: true"><i class='fa fa-remove fa-120 pt-5 pb-4 fa-c-red'></i></a>
				 	<a onClick="$parentResource.combotree('reload')" class="easyui-linkbutton ml-3" data-options="plain: true"><i class='fa fa-refresh fa-120 pt-5 pb-4 fa-c-green'></i></a>
				</td>
			</tr>
			<tr>
				<th>资源名称：</th>
				<td><input name="name" class="easyui-validatebox" type="text" style="width: 261px;" /></td>
			</tr>
			<tr>
				<th>权限标识：</th>
				<td><input name="identity" class="easyui-validatebox" type="text" style="width: 261px;" /></td>
			</tr>
			<tr>
				<th>资源地址：</th>
				<td>
					<textarea name="href" style="width:259px;height:50px;padding:4px;"></textarea>
				</td>
			</tr>
			<tr id="weight" style="display: none;">
				<th>资源排序：</th>
				<td><input name="weight" class="easyui-numberspinner spinner-text" data-options="min:1" value="0" style="width:269px;height:25px;padding-top:3px;" /></td>
			</tr>
			<tr>
				<th>资源图标：</th>
				<td><input name="icon" class="easyui-validatebox" type="text" style="width: 261px;" /></td>
			</tr>
			<tr>
				<th>打开方式：</th>
				<td>
					<div id="icheck">
						<input id="iniframe1" type="radio" name="iniframe" value="true" checked="checked">
						<label class="irl" for="iniframe1">IFrame</label>　
						<input id="iniframe2" type="radio" name="iniframe" value="false">
						<label class="irl" for="iniframe2">Local</label>
					</div>
				</td>
			</tr>
			<tr>
				<th>展开/折叠：</th>
				<td>
					<div id="icheck">
						<input id="state1" type="radio" name="state" value="open" checked="checked">
						<label class="irl" for="state1">展开</label>　
						<input id="state2" type="radio" name="state" value="closed">
						<label class="irl" for="state2">折叠</label>
					</div>
				</td>
			</tr>
			<tr>
				<th>资源类型：</th>
				<td>
					<div id="icheck">
						<input id="type1" type="radio" name="type" value="false" checked="checked">
						<label class="irl" for="type1">普通资源</label>　
						<input id="type2" type="radio" name="type" value="true">
						<label class="irl" for="type2">操作资源</label>　
						
						<span id="initCrudSpan">
							<input id="initCrud" type="checkbox" name="initCrud" value="true"> <label class="irl" for="input-2">初始化【增／删／改／查】</label>
						</span>
					</div>
				</td>
			</tr>
			<tr style="display:none">
				<td>
					<input type="hidden" name="id" value="${id}">
					<input type="hidden" name="parentIds" value="${id}">
					<input type="hidden" name="parentIdentity" value="${id}">
				</td>
			</tr>
		</table>
	</form>
</div>

<script>
	var form_url = "${ctx}/admin/sys/resource/add", id = "${id}", $parentResource, $menuDialog ;
	$(function(){
		$('#icheck input').iCheck({
			checkboxClass: 'icheckbox_minimal-blue',
			radioClass: 'iradio_square-blue'
	   	});
		$('#icheck input[name=type]').on('ifChecked', function(event){
			if($(event.currentTarget).val() == "true") {
				$("textarea[name=href]").val("");
				$("#initCrud").iCheck('uncheck');
				$("#initCrudSpan").hide();
			} else {
				if(undefined == id || "" == id) {
					$("#initCrud").iCheck('check');
					$("#initCrudSpan").show();
				}
			}
		});
		
		$menuDialog = $("#menuDialog").combobox({
			url: "${ctx}/admin/sys/menu/listAll",
			valueField: "id", textField: "name", panelHeight: "auto",
			onSelect: function(record) {
				$parentResource.combotree("reload", "${ctx}/admin/sys/resource/tree?menuId="+record.id+"&filterType=true");
			},
			onLoadSuccess: function() {
				$menuDialog.combobox("setValue", "${menuId}");
				$parentResource.combotree("reload", "${ctx}/admin/sys/resource/tree?menuId=${menuId}&filterType=true");
			}
		});
		
		$parentResource = $("#pid").combotree({
			idFiled:'pid', editable: false, required:false, lines:true, autoShowPanel: false,
			onSelect: function(node) {
				var parentIds = ((null==node.parentIds||""==node.parentIds?"":node.parentIds+"/"));
				var parentIdentity = ((null==node.parentIdentity||""==node.parentIdentity?"":node.parentIdentity+":"));
				$("input[name=parentIds]").val(parentIds + node.id);
				$("input[name=parentIdentity]").val(parentIdentity + node.identity);
			}
		});
		
		if(undefined != id && "" != id) {
			$("#weight").show();
			$("#initCrud").iCheck('uncheck');
			$("#initCrudSpan").hide();
			
			form_url = "${ctx}/admin/sys/resource/update" ;
			$.ajax({
				type: "GET", dataType: "JSON",
				url: "${ctx}/admin/sys/resource/get/"+id,
				success: function(result) {
					$("input[name=iniframe][value="+result.iniframe+"]").iCheck('check') ;
					$("input[name=type][value="+result.type+"]").iCheck('check') ;
					$("input[name=state][value="+result.state+"]").iCheck('check') ;
					$("#form").form("load", result);
				},
				complete: function(XMLHttpRequest, textStatus){},
				error: function(){
					$.easyui.loaded();
					alert("请求出错处理") ;
				}
			});
		}
		
	});
	
	//提交表单数据
	var submitNow = function($d, $tg, flag) {
		$.post(form_url, $("#form").form("getData"), function(result) {
			if (result.status) {
				$tg.treegrid('reload');alertify.success(result.message);$.easyui.loaded();
				if(flag==undefined||flag==false){$d.dialog("close") ;}
			} else {
				$.easyui.loaded();$.messager.alert("错误", result.message, "error");
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	};
	
	//验证表单
	var submitForm = function($d, $tg, flag) { 
		if($('#form').form('validate')) {
			$.easyui.loading({ msg: "数据提交中，请稍等..." });
			submitNow($d, $tg, flag) ;
		}
	};
</script>

